package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.GiftDto;
import com.csun.cmny.provider.dto.GiftQueryDto;
import com.csun.cmny.provider.model.domain.OmcGift;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcGiftService;
import com.csun.cmny.provider.validator.OmcGiftAddValidator;
import com.csun.cmny.provider.validator.OmcGiftEditValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-05-23 10:11
 * @Description: the class omc gift controller
 */
@RestController
@RequestMapping(value = "/gift", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcGiftController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcGiftService omcGiftService;

    /**
     * 礼品添加
     *
     * @param giftDto   gift dto
     * @param br        binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addGift(@Validated(OmcGiftAddValidator.class) @RequestBody GiftDto giftDto,
                                 BindingResult br) {

        // 设置组织Id
        giftDto.setGroupId(getLoginAuthDto().getGroupId());

        Integer result = saveGift(giftDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 礼品修改
     *
     * @param giftDto   gift dto
     * @param br        binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editGift(@Validated(OmcGiftEditValidator.class) @RequestBody GiftDto giftDto,
                            BindingResult br) {

        Integer result = saveGift(giftDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 礼品删除
     *
     * @param giftId    gift id
     *
     * @return  the wrapper.
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/deleteById/{giftId}", method = RequestMethod.POST)
    public Wrapper deleteGift(@PathVariable Long giftId) {

        // 校验giftId是否为空
        Preconditions.checkArgument(giftId != null, cmnyMessageSource
                .getMessage("gift.id.null", null));

        Integer result = omcGiftService.deleteGiftById(giftId);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 礼品分页查询
     *
     * @param giftQueryDto
     * @return
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryGiftListWithPage(@RequestBody GiftQueryDto giftQueryDto) {

        // 设置组织Id
        giftQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        PageInfo pageInfo = omcGiftService.queryGiftListWithPage(giftQueryDto);

//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 礼品保存
     *
     * @param giftDto   gift dto
     *
     * @return  the int.
     */
    public Integer saveGift(GiftDto giftDto) {
        OmcGift omcGift = new OmcGift();
        BeanUtils.copyProperties(giftDto, omcGift);
        return omcGiftService.saveGift(omcGift, getLoginAuthDto());
    }

}
